﻿using System;

namespace eCommerce.SagePay
{
    public class SagePayHelper
    {
        /// <summary>
        /// Gets the full status details.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="statusDetail">The status detail.</param>
        /// <returns></returns>
        public static string GetFullStatusDetails(string status, string statusDetail)
        {
            switch (status)
            {
                case "OK":
                    return "AUTHORISED - The transaction was successfully authorised with the bank.";
                case "MALFORMED":
                    return "MALFORMED - The StatusDetail was:" + statusDetail;
                case "INVALID":
                    return "INVALID - The StatusDetail was:" + statusDetail;
                case "NOTAUTHED":
                    return "DECLINED - The transaction was not authorised by the bank.";
                case "REJECTED":
                    return "REJECTED - The transaction was failed by your 3D-Secure or AVS/CV2 rule-bases.";
                case "AUTHENTICATED":
                    return "AUTHENTICATED - The transaction was successfully 3D-Secure Authenticated and can now be Authorised.";
                case "REGISTERED":
                    return "REGISTERED - The transaction was could not be 3D-Secure Authenticated, but has been registered to be Authorised.";
                case "ERROR":
                    return "ERROR - There was an error during the payment process.  The error details are: " + statusDetail;
                default:
                    return "UNKNOWN - An unknown status was returned from Sage Pay.  The Status was: " + status + ", with StatusDetail:" + statusDetail;
            }
        }

        /// <summary>
        /// Finds the field.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        public static string FindField(string fieldName, string response)
        {
            string result = string.Empty;
            string[] split = response.Split(Environment.NewLine.ToCharArray());

            foreach (string status in split)
            {
                if (status.Contains(fieldName))
                {
                    if (status.Substring(fieldName.Length, 1) == "=")
                    {
                        result = status.Substring(fieldName.Length + 1);
                    }
                }
            }

            return result;
        }

        #region SystemUrl
        /// <summary>
        /// Systems the URL.
        /// </summary>
        /// <param name="connectTo">The connect to.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static string SystemUrl(string connectTo, string type)
        {
            string systemUrl = "";
            if (connectTo == "LIVE")
            {
                switch (type)
                {
                    case "abort":
                        systemUrl = "https://live.sagepay.com/gateway/service/abort.vsp";
                        break;
                    case "authorise":
                        systemUrl = "https://live.sagepay.com/gateway/service/authorise.vsp";
                        break;
                    case "cancel":
                        systemUrl = "https://live.sagepay.com/gateway/service/cancel.vsp";
                        break;
                    case "purchase":
                        systemUrl = "https://live.sagepay.com/gateway/service/vspdirect-register.vsp";
                        break;
                    case "refund":
                        systemUrl = "https://live.sagepay.com/gateway/service/refund.vsp";
                        break;
                    case "release":
                        systemUrl = "https://live.sagepay.com/gateway/service/release.vsp";
                        break;
                    case "repeat":
                        systemUrl = "https://live.sagepay.com/gateway/service/repeat.vsp";
                        break;
                    case "void":
                        systemUrl = "https://live.sagepay.com/gateway/service/void.vsp";
                        break;
                    case "3dcallback":
                        systemUrl = "https://live.sagepay.com/gateway/service/direct3dcallback.vsp";
                        break;
                    case "showpost":
                        systemUrl = "https://test.sagepay.com/showpost/showpost.asp";
                        break;
                    case "paypal":
                        systemUrl = "https://live.sagepay.com/gateway/service/complete.vsp";
                        break;
                }
            }
            else if (connectTo == "TEST")
            {
                switch (type)
                {
                    case "abort":
                        systemUrl = "https://test.sagepay.com/gateway/service/abort.vsp";
                        break;
                    case "authorise":
                        systemUrl = "https://test.sagepay.com/gateway/service/authorise.vsp";
                        break;
                    case "cancel":
                        systemUrl = "https://test.sagepay.com/gateway/service/cancel.vsp";
                        break;
                    case "purchase":
                        systemUrl = "https://test.sagepay.com/gateway/service/vspdirect-register.vsp";
                        break;
                    case "refund":
                        systemUrl = "https://test.sagepay.com/gateway/service/refund.vsp";
                        break;
                    case "release":
                        systemUrl = "https://test.sagepay.com/gateway/service/release.vsp";
                        break;
                    case "repeat":
                        systemUrl = "https://test.sagepay.com/gateway/service/repeat.vsp";
                        break;
                    case "void":
                        systemUrl = "https://test.sagepay.com/gateway/service/void.vsp";
                        break;
                    case "3dcallback":
                        systemUrl = "https://test.sagepay.com/gateway/service/direct3dcallback.vsp";
                        break;
                    case "showpost":
                        systemUrl = "https://test.sagepay.com/showpost/showpost.asp";
                        break;
                    case "paypal":
                        systemUrl = "https://test.sagepay.com/gateway/service/complete.vsp";
                        break;
                }
            }
            else
            {
                switch (type)
                {
                    case "abort":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorAbortTx";
                        break;
                    case "authorise":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorAuthoriseTx";
                        break;
                    case "cancel":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorCancelTx";
                        break;
                    case "purchase":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp";
                        break;
                    case "refund":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorRefundTx";
                        break;
                    case "release":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorReleaseTx";
                        break;
                    case "repeat":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorRepeatTx";
                        break;
                    case "void":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectGateway.asp?Service=VendorVoidTx";
                        break;
                    case "3dcallback":
                        systemUrl = "https://test.sagepay.com/simulator/VSPDirectCallback.asp";
                        break;
                    case "showpost":
                        systemUrl = "https://test.sagepay.com/showpost/showpost.asp";
                        break;
                    case "paypal":
                        systemUrl = "https://test.sagepay.com/simulator/paypalcomplete.asp";
                        break;
                }
            }

            return systemUrl;
        }
        #endregion
    }
}
